package spring.superrun.record.mvc.controller;

import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import spring.superrun.record.mvc.attribute.AllowedAnyone;
import spring.superrun.record.mvc.dto.CompleteToken;
import spring.superrun.record.mvc.dto.WeChatInfoUpdateDto;
import spring.superrun.record.mvc.dto.WeChatLoginResultDto;
import spring.superrun.record.mvc.dto.WeChatMinLoginDto;
import spring.superrun.record.mvc.entity.SuperrunUserinfos;
import spring.superrun.record.mvc.entity.enumlibs.ServiceResultCode;
import spring.superrun.record.mvc.entity.response.ServiceResult;
import spring.superrun.record.mvc.entity.response.ServiceResultOfT;
import spring.superrun.record.mvc.service.JwtTokenService;
import spring.superrun.record.mvc.service.SuperrunUserinfosService;
import spring.superrun.tool.newid.Encrypt;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

@RestController
@RequestMapping("/api/wechat_account")
public class WechatAppController {
    @Resource
    private SuperrunUserinfosService _userInfoService;
    @Resource
    private JwtTokenService _jwtService;

    @ApiOperation("微信登录接口")
    @PostMapping("wlogin")
    @ResponseBody
    @AllowedAnyone
    public ResponseEntity<ServiceResultOfT<WeChatLoginResultDto>> WeChatMinLogin
            (@RequestBody WeChatMinLoginDto model){
        ServiceResultOfT<WeChatLoginResultDto> result = new ServiceResultOfT<>();
        ServiceResultOfT<SuperrunUserinfos> getWeChatResult =
                _userInfoService.GetWechatUserByCodeFromTencent(model);
        result.IsFailed(getWeChatResult.getCode(), getWeChatResult.getMessage());

        if (!getWeChatResult.Success)
            return getWeChatResult.getCode().equals(ServiceResultCode.NotFound)
                    ?new ResponseEntity<>(result, HttpStatus.NOT_FOUND)
                    :new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        WeChatLoginResultDto loginResult =
                new WeChatLoginResultDto(getWeChatResult.getEntities());
        //只要有一个未填写的信息就是未更新
        if (!getWeChatResult.getEntities().VerifyOtherInfo())
        {
            result.IsSuccess(loginResult,"获取微信用户信息成功但未更新个人信息!");
            return new ResponseEntity<>(result, HttpStatus.OK);
        }
        getWeChatResult.getEntities().IsUpdateOtherInfo=getWeChatResult.getEntities().VerifyOtherInfo();
        //创建token
        CompleteToken token =  _jwtService.CreateToken(getWeChatResult.getEntities());
        loginResult.setToken(token);
        loginResult.setUser(getWeChatResult.getEntities());
        result.IsSuccess(loginResult, "获取微信用户信息成功并返回token");
        return new ResponseEntity<>(result,HttpStatus.OK);
    }

    @ApiOperation("更新微信用户信息")
    @PutMapping(value = "/{id}")
    public ResponseEntity<ServiceResult>UpdateWeChatOtherInfo(
            @PathVariable @NotEmpty String id
            ,@RequestBody @Valid WeChatInfoUpdateDto dto) throws Exception {
        ServiceResult result = new ServiceResult();
        String userId = Encrypt.decryptAES(Encrypt.decryptBASE64(id));
        result.IsFailed(ServiceResultCode.KeyIsNull,"关键信息为空!!!");
        SuperrunUserinfos user =_userInfoService.queryById(userId);
        if (null == user) return new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
        user.SaveOtherInfosNoSex(dto);
        user = _userInfoService.update(user);
        if (null!=user)
        {
//            await _redisCommonService.IncrByAsync(RedisKeyValues.WeChatRegisetCountKey, 1L);
            result.IsSuccess("成功更新微信用户");
            new ResponseEntity<>(result,HttpStatus.OK);
        }
        return new ResponseEntity<>(result,HttpStatus.BAD_REQUEST);
    }
}
